package cn.kgc.vueprohou.controller;

import cn.kgc.vueprohou.pojo.Product;
import cn.kgc.vueprohou.pojo.ResultEntity;
import cn.kgc.vueprohou.service.ProductService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@Api(tags = "商品相关接口",description = "提供用户相关的Rest API")
public class ProController {
    @Resource
    ProductService productService;

    @RequestMapping("/selePro")
    @ApiOperation("查询商品信息")
    @ApiResponses({
            @ApiResponse(code = 400,message = "请求参数问题"),
            @ApiResponse(code = 404,message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 405,message = "请求类型不正确"),
            @ApiResponse(code = 500,message = "后端程序出错")
    })
    public ResultEntity<List<Product>> selePro(){
        List<Product> sele = productService.sele();
    return ResultEntity.successWithoutData(sele);
    }

    @RequestMapping("/seleById")
    @ApiOperation("根据id查询商品信息")
    @ApiImplicitParam(name="id",value = "用户id",required = true)
    @ApiResponses({
            @ApiResponse(code = 400,message = "请求参数问题"),
            @ApiResponse(code = 404,message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 405,message = "请求类型不正确"),
            @ApiResponse(code = 500,message = "后端程序出错")
    })
    public ResultEntity<Product> seleById(Integer id){
        Product product = productService.seleById(id);
        System.out.println("数量"+product.getQuantity());
    return ResultEntity.successWithoutData(product);
    }

    @RequestMapping("/up")
    @ApiOperation("修改商品库存数量!")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "商品id",required = true),
            @ApiImplicitParam(name ="count",value = "出库数量",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 400,message = "请求参数问题"),
            @ApiResponse(code = 404,message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 405,message = "请求类型不正确"),
            @ApiResponse(code = 500,message = "后端程序出错")
    })
    public ResultEntity<String> up(Integer id,Integer count){
        Product product = productService.seleById(id);
        Integer newquantiy=product.getQuantity()-count;
        product.setQuantity(newquantiy);
        Integer up = productService.up(product);
        return ResultEntity.successWithoutData();
    }


}
